<?php  
class FormularioArticulo extends sfForm
{
	public function __construct($id, $defatults=array(), $options = array(), $CSRFSecret = null)
	{
		//$articulo = ArticuloPeer::retrieveByPK($id);
		$articulo = ProductsPeer::retrieveByPK($id);
		//$this->articulo_colores = ArticuloPeer::getColores($articulo->getCodigo()); 
		//$this->articulo_tallas = ArticuloPeer::getTallas($articulo->getCodigo());
		
		//$this->widgetSchema['precio']->setAttribute('value',ticuloPeer::retrieveByPK($id)->getPrecio());
		
		//$this->codigo = $articulo->getCodigo();
		$this->codigo = $articulo->getCode();
		$this->id = $id;
		//$this->precio = $articulo->getPrecio();
		$this->precio = $articulo->getPricesell();
		parent::__construct($defatults=array(), $options = array(), $CSRFSecret = null);		
	}
  public function configure()
  {
    $this->setWidgets(array(
      'id'    => new sfWidgetFormInputHidden(),
      'codigo'    => new sfWidgetFormInputHidden(),
      'precio'=> new sfWidgetFormInputHidden(),
      'cantidad'     => new sfWidgetFormInput(array(),array('size'=>'2')),
      //'color'   => new sfWidgetFormSelectRadio(array('choices'=> $this->articulo_colores,'separator'=>' ','formatter'=>array($this,'formato') ) ),
      //'talla'   => new sfWidgetFormSelect(array('choices'=>$this->articulo_tallas ) ),
    ));
    //$this->setDefault('color','0');
    $this->setDefault('cantidad','1');
    $this->setDefault('id',$this->id);
    $this->setDefault('codigo',$this->codigo);
    $this->setDefault('precio',"$this->precio");
    $this->widgetSchema->setNameFormat('articulo[%s]');
    $this->widgetSchema->setFormFormatterName('table');
    
    $this->setValidators(array(
      'id'=> new sfValidatorString(array('required'=>true)),
      'codigo'=> new sfValidatorString(array('required'=>true)),
      'cantidad'=> new sfValidatorInteger(array('required'=>true,'min'=>1,'trim'=>true)) ,
      'precio'=> new sfValidatorNumber(array('required'=>true,'min'=>$this->precio,'max'=>$this->precio)) ,
      //'color'=> new sfValidatorChoice(array('required'=>false,'choices'=>array_keys( $this->articulo_colores))),
      //'talla'=> new sfValidatorChoice(array('required'=>false,'choices'=>array_keys( $this->articulo_tallas ))),
    ));
    
  }
  
  public function formato($widget, $inputs){
  	$rows = array();
    foreach ($inputs as $input)
    {
    	$txt = ereg_replace("<label (.)+>#",'#',$input['label']);
    	$txt = ereg_replace("</label>","",$txt);
    	$input['label']='&nbsp;&nbsp;&nbsp;';
      $rows[] = $this->widgetSchema['color']->renderContentTag('span', $input['input'].$this->getOption('label_separator').$input['label'],array('style'=> "background-color: $txt")); 
    }
 
     return $this->widgetSchema['color']->renderContentTag('div', implode($this->getOption('separator'), $rows), array('class' => '_list'));  	
  }
}
